<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:if test="${empty pageHandler}">
    <c:set var="pageHandler" value="onPageChange" />
</c:if>

<c:set var="totalPages" value="${page.totalPages}" />
<c:set var="totalElements" value="${page.totalElements}" />
<c:set var="number" value="${page.number + 1}" />
<c:set var="size" value="${page.size}" />

<c:set var="visiblePages" value="9" /> <!-- 总共显示的页码 value="奇数 并且 大于等于5" -->
<c:set var="visiblePagesHalf" value="${(visiblePages - 1) / 2}" />  <!-- 当前页左右的页码 -->
<c:set var="begin" value="1" /> <!-- 默认起始页 -->
<c:set var="end" value="${totalPages}" />   <!-- 默认结束页 -->

<style type="text/css">
    .number {
        background-color: #eee;
        color: #fff;
    }
    .page-color {
        color: #337AB7;
        border-color: #e9e9e9;
    }
    .btn-default.active, .btn-default:active, .btn-default:hover, .open > .btn-default.dropdown-toggle {
        color: #337AB7;
        background-color: #efefef;
        border-color: #ddd;
    }
</style>

<c:if test="${page.content.size() == 0 && number > 1}">
    <script type="text/javascript">
        (function () {
            eval("${pageHandler}(" + ${number - 1} + ")");
        })();
    </script>
</c:if>

<div class="btn-group pull-right" data-page-handler="${pageHandler}">

    <c:if test="${totalPages > visiblePages}">
        <c:if test="${number + visiblePagesHalf <= totalPages}">
            <c:set var="end" value="${number + visiblePagesHalf}" />
            <c:if test="${end <= visiblePages}">
                <c:set var="end" value="${visiblePages}" />
            </c:if>
        </c:if>
        <c:if test="${number - visiblePagesHalf > 0}">
            <c:set var="begin" value="${number - visiblePagesHalf}" />
            <c:if test="${end - begin + 1 < visiblePages}">
                <c:set var="begin" value="${end - visiblePages + 1}" />
            </c:if>
        </c:if>
    </c:if>

    <a href="javascript:;" title="首页" onclick="<c:if test='${number != 1}'>${pageHandler}(1)</c:if>;" class="btn btn-default page-color ${number == 1 ? 'disabled disabled-link' : ''}"><i class="fa fa-caret-left"></i><i class="fa fa-caret-left"></i></a>
    <a href="javascript:;" title="上一页" onclick="${pageHandler}(${number - 1 > 1 ? number - 1 : 1});" class="btn btn-default page-color ${number - 1 < 1 ? 'disabled disabled-link' : ''}"><i class="fa fa-caret-left"></i></a>

    <%--<c:if test="${begin > 1}">--%>
        <%--<a href="javascript:;" onclick="${pageHandler}(1);" class="btn btn-default page-color">1</a>--%>
        <%--<c:set var="begin" value="${begin + 1}" />--%>
    <%--</c:if>--%>

    <c:forEach begin="${begin}" end="${end}" varStatus="status">
        <c:choose>
            <c:when test="${status.index == end && end < totalPages}">
                <%--<a href="javascript:;" onclick="${pageHandler}(${totalPages});" class="btn btn-default page-color">${totalPages}</a>--%>
            </c:when>
            <c:otherwise>
                <a href="javascript:;" onclick="<c:if test="${status.index != number}">${pageHandler}(${status.index});</c:if>" class="btn btn-default page-color ${status.index == number ? 'blue' : ''}">${status.index}</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>

    <a href="javascript:;" title="下一页" onclick="${pageHandler}(${number + 1 < totalPages ? number + 1 : totalPages});" class="btn btn-default page-color ${number + 1 > totalPages ? 'disabled disabled-link' : ''}"><i class="fa fa-caret-right"></i></a>
    <a href="javascript:;" title="末页：${totalPages}" onclick="<c:if test='${number != totalPages}'>${pageHandler}(${totalPages})</c:if>;" class="btn btn-default page-color ${number == totalPages ? 'disabled disabled-link' : ''}"><i class="fa fa-caret-right"></i><i class="fa fa-caret-right"></i></a>
</div>
<div class="clearfix"></div>